package com.stxx.stiot2.controller;

import com.stxx.framwork.domain.R;
import com.stxx.framwork.domain.model.PageQuery;
import com.stxx.framwork.domain.model.PageResult;
import com.stxx.stiot2.domain.Gateway;
import com.stxx.stiot2.gateway.GatewayUtils;
import com.stxx.stiot2.service.GatewayService;
import org.noear.solon.annotation.*;

import java.sql.SQLException;
import java.util.List;

@Controller
@Mapping("/gateway")
public class GatewayController {

    @Inject
    GatewayService baseService;
    @Inject
    GatewayUtils gatewayUtils;

    @Get
    @Mapping("/list")
    public PageResult<Gateway> list(Gateway bo, PageQuery pageQuery) throws SQLException {
        PageResult<Gateway> r = baseService.pageList(bo,pageQuery);
        return r;
    }


    @Post
    @Mapping("")
    public R<Object> add(@Body Gateway bo) throws SQLException {
        baseService.insert(bo);
        return R.ok("数据保存成功");
    }

    @Put
    @Mapping("")
    public R<Object> edit(@Body Gateway bo) throws SQLException {
        baseService.update(bo);
        return R.ok("数据保存成功");
    }

    @Delete
    @Mapping("/{ids}")
    public R<Object> remove(@Path String[] ids) throws SQLException {
        baseService.deleteByIds(ids);
        return R.ok("数据删除成功");
    }

    @Get
    @Mapping("/active/{code}")
    public R<Void> active(@Path String code) throws SQLException {
        Gateway gateway = baseService.queryById(code);
        gatewayUtils.remove(code);
        gatewayUtils.initGateway(gateway);
        gateway.setActive(true);
        baseService.update(gateway);
        return R.ok();
    }

    @Get
    @Mapping("/inactive/{code}")
    public R<Void> unactive(@Path String code) throws SQLException {
        Gateway gateway = baseService.queryById(code);
        gatewayUtils.remove(code);
        gateway.setActive(false);
        baseService.update(gateway);
        return R.ok();
    }
}
